/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package routine;

import routine.utility.NoteRow;
import routine.utility.RoutineManager;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Wizard
 */
public class RoutineForm extends javax.swing.JDialog 
{

    /**
     * Creates new form RoutineForm
     */
    public RoutineForm() 
    {
        initComponents();
        this.setLocationRelativeTo(null);
        this.setModal(true);
        routineTable.getTableHeader().setReorderingAllowed(false);
        routineTable.getTableHeader().setResizingAllowed(false);
        settingForm = new SettingForm();
    }

    public void setDate(int year, int month, int day)
           throws java.sql.SQLException, ClassNotFoundException
    {
        if(this.year == year && this.month == month && this.day == day)
            return;
        this.year = year;
        this.month = month;
        this.day = day;
        this.setTitle("日程安排 " + String.valueOf(year) + "年" +
                      String.valueOf(month) + "月" + String.valueOf(day) + "日");
        routine.load(year, month, day);
        showRoutine();
        this.year = year;
        this.month = month;
        this.day = day;
    }
    
    private void showRoutine()
    {
        DefaultTableModel model = (DefaultTableModel)routineTable.getModel();
        model.setRowCount(0);
        for(int i = 0; i < routine.size(); i++)
        {
            NoteRow note = routine.get(i);
            Object[] arr = new Object[6];
            arr[0] = note.getTitle();
            arr[1] = RoutineManager.timeConvert(note.getStartTime());
            arr[2] = RoutineManager.timeConvert(note.getEndTime());
            int itype = note.getAlertType();
            String strtype;
            if(itype == 1)
                strtype = "提前一天";
            else if(itype == 2)
                strtype = "当天";
            else
                strtype = "无";
            arr[3] = strtype;
            arr[4] = RoutineManager.timeConvert(note.getAlertTime());
            arr[5] = note.getComment();
            model.addRow(arr);
        }
    }
    
    public void clear()
    {
        year = month = day = 0;
        this.routine.clear();
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        routineScrollPane = new javax.swing.JScrollPane();
        routineTable = new javax.swing.JTable();
        delButton = new javax.swing.JButton();
        addButton = new javax.swing.JButton();

        setResizable(false);

        routineTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "事件名称", "起始时间", "截止时间", "提醒方式", "提醒时间", "备注"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        routineTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                routineTable_Click(evt);
            }
        });
        routineScrollPane.setViewportView(routineTable);

        delButton.setText("删除");
        delButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                delButton_Click(evt);
            }
        });

        addButton.setText("添加");
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButton_Click(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(routineScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 627, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(addButton, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(delButton, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(routineScrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 301, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(delButton)
                    .addComponent(addButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void routineTable_Click(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_routineTable_Click
        try
        {
            if(evt.getClickCount() == 1) return;
            int row = routineTable.getSelectedRow();
            NoteRow note = routine.get(row).clone();
            settingForm.setNote(note);
            settingForm.setVisible(true);
            if(settingForm.isSaved())
            {
                routine.updateAt(row, note);
                showRoutine();
            }
        }
        catch(Exception ex)
        {
            JOptionPane.showMessageDialog(null, "修改失败！" + ex.getMessage());
        }
    }//GEN-LAST:event_routineTable_Click

    private void delButton_Click(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_delButton_Click
        try
        {
            int row = routineTable.getSelectedRow();
            if(row == -1) return;
            if(JOptionPane.showConfirmDialog(null, "真的要删除吗？",
                UIManager.getString("OptionPane.titleText") , 
                JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION)
                return;
            routine.removeAt(row);
            showRoutine();
        }
        catch(Exception ex)
        {
            JOptionPane.showMessageDialog(null, "删除失败！" + ex.getMessage());
        }
    }//GEN-LAST:event_delButton_Click

    private void addButton_Click(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButton_Click
        try
        {
            NoteRow note = new NoteRow();
            note.setDate(year * 10000 + month * 100 + day);
            settingForm.setNote(note);
            settingForm.setVisible(true);
            if(settingForm.isSaved())
            {
                routine.add(note);
                showRoutine();
            }
        }
        catch(Exception ex)
        {
            JOptionPane.showMessageDialog(null, "添加失败！" + ex.getMessage());
        }
    }//GEN-LAST:event_addButton_Click


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addButton;
    private javax.swing.JButton delButton;
    private javax.swing.JScrollPane routineScrollPane;
    private javax.swing.JTable routineTable;
    // End of variables declaration//GEN-END:variables

    private int year;
    private int month;
    private int day;
    private RoutineManager routine
            = new RoutineManager();
    private SettingForm settingForm;
}